package common

import (
    "github.com/astaxie/beego/orm"
	_ "github.com/mattn/go-sqlite3"
	"beego/testApp/common/tool"
	"time"
)

type Menu struct {
 
	Mid int  `orm:"pk;auto;column(Mid)"`
	Mname string
	Url string
	Sort int
	Created time.Time
}

func init(){
   
	tool.Pic("Menu pakcage init"," init")
  
	orm.RegisterModel(new(Menu))
	

}

func (m *Menu) Del( mid int) bool {

	tool.Pic("del men ","in ")

    o := orm.NewOrm()
	o.Using("default") 

	m.Mid = mid
	
	num,err := o.Delete(m)

	if(err != nil){
	   tool.Pic("men del fail",err)
	   return false
	}

	tool.Pic("menu del success affect rows", num )

	return true
	 
}

func (m *Menu) GetMenuList() []Menu {

	o := orm.NewOrm()
	o.Using("default")
	var menus []Menu

	qs := o.QueryTable("menu")

	num,err := qs.All(&menus)
   
	if(err!=nil){
	   tool.Pic("get menu qs  error",err)
	   return menus
	}
  
	tool.Pic("get menu list success",num)  

	return menus
}

func (m *Menu) Add() bool {

	o := orm.NewOrm()
	o.Using("default")
	id,err  := o.Insert(m)
	
	if(err!=nil){
	   
		tool.Pic("Menu add() insert into Db FAIL",err)
        return false
	} 

	tool.Pic("Menu add() insert into db succsess ",id)

   return true
}



